﻿using System.Windows;

namespace SmartPlant.CADView
{
    /// <summary>
    /// CAD预览控件文件MVVM属性绑定帮助类。
    /// </summary>
    public static class CADViewControlHelper
    {
        public static readonly DependencyProperty DwgFileProperty =
              DependencyProperty.RegisterAttached("DwgFile", typeof(string),
                  typeof(CADViewControlHelper),
                  new FrameworkPropertyMetadata(string.Empty, PropertyChangedCallback));

        /// <summary>
        /// 设置<see cref="CADViewControl"/>控件DWG属性。
        /// </summary>
        /// <param name="dp"></param>
        /// <param name="value"></param>
        public static void SetDwgFile(DependencyObject dp, string value)
        {
            dp.SetValue(DwgFileProperty, value);
        }

        /// <summary>
        /// 获取<see cref="CADViewControl"/>控件DWG属性。
        /// </summary>
        /// <param name="dp"></param>
        /// <returns></returns>
        public static string GetDwgFile(DependencyObject dp)
        {
            return (string)dp.GetValue(DwgFileProperty);
        }

        private static void PropertyChangedCallback(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            if (sender is CADViewControl control)
            {
                var dwgFile = e.NewValue != null ? e.NewValue.ToString() : "";
                control.OnOpenAndRenderDwgFile(dwgFile);
            }
        }
    }
}
